package com.darren.befriend.fragments;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.darren.befriend.utilities.LogUtil;
import com.darren.befriend.utilities.MySharedPreferences;
import com.darren.befriend.utilities.Utilities;
import com.darren.befriend.views.MyProgressDialog;
import com.parse.ParseUser;

public class ProfileFragment extends ProfileBaseFragment {

	public static ProfileFragment makeInstance(ParseUser user) {
		ProfileFragment fragment = new ProfileFragment();
		mUsr = user;
		mVisibleLike = false;
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = super.onCreateView(inflater, container, savedInstanceState);
		mLikeBtn.setVisibility(View.VISIBLE);
		mDislikeBtn.setVisibility(View.VISIBLE);
		String userPassword = MySharedPreferences.getPreferences(
				MySharedPreferences.PREF_PASSWORD, "");
		if (!userPassword.equals("")) {

			mUploadImageBtn.setVisibility(View.VISIBLE);
		}

		return v;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onActivityResult(int, int,
	 * android.content.Intent)
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case REQUEST_CAMERA:
			case REQUEST_GALERY:

				new UploadDataSyncTask().execute(data);
				break;
			}
		}
	}

	private class UploadDataSyncTask extends AsyncTask<Intent, Void, Bitmap> {

		private Dialog dialog;

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = MyProgressDialog.show(getActivity(), "", "", false, false);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#doInBackground(java.lang.Object[])
		 */
		@Override
		protected Bitmap doInBackground(Intent... params) {
			// TODO Auto-generated method stub

			Intent data = params[0];
			InputStream stream;
			Bitmap bitmap = null;
			try {

				stream = getActivity().getContentResolver().openInputStream(
						data.getData());
				bitmap = BitmapFactory.decodeStream(stream);
				stream.close();

				// Rotate image
				try {
					ExifInterface ei = new ExifInterface(data.getData()
							.getPath());
					int orientation = ei.getAttributeInt(
							ExifInterface.TAG_ORIENTATION,
							ExifInterface.ORIENTATION_NORMAL);
					switch (orientation) {
					case ExifInterface.ORIENTATION_ROTATE_90:
						Utilities.rotateBitmap(bitmap, 90);
						break;
					case ExifInterface.ORIENTATION_ROTATE_180:
						Utilities.rotateBitmap(bitmap, 180);
						break;
					case 0:

						Utilities.rotateBitmap(bitmap, 90);
						break;
					}
				} catch (Exception ex) {

				}

				ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
				bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteStream);
				LogUtil.e("Befroe size", byteStream.size() + "");
				if (byteStream.size() > 128000) {

					bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
				}

				byte[] image = byteStream.toByteArray();
				LogUtil.e("Image size", image.length + "");
				if (image.length > 128000) {

					return null;
				}

				mCurrentUsr.fetchIfNeeded();
				mCurrentUsr.put("Status", true);
				mCurrentUsr.put("ProfilePic", "ProfilePic");
				mCurrentUsr.put("Photo", image);
				mCurrentUsr.save();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				mCurrentUsr.remove("ProfilePic");
				mCurrentUsr.remove("Photo");
				return null;
			}

			return bitmap;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Bitmap result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {

				mProfilePicView.setImageBitmap(result);
			} else {

				Utilities.showAlertMessage(getActivity(),
						"Your image too large!", "");
			}

			try {

				dialog.dismiss();
			} catch (Exception ex) {

				LogUtil.e("Dismis dialog", ex.getMessage());
			}
		}
	}
}
